# Copyright 1999-2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2 
# $Header : $

inherit eutils libtool check-kernel  64-bit

IUSE="ffmpeg gd dvd a52 cdparanoia jpeg encode imagemagick lirc live mad mpeg2dec ncurses dvb qt shout vorbis fftw xercesc l1394 alsa mmbox"

DESCRIPTION="Network-Integrated Multimedia Middleware NMM"
HOMEPAGE="http://www.networkmultimedia.org"
SRC_URI="http://graphics.cs.uni-sb.de/NMM/dist-0.9.1/Download/${P}.tar.gz"

SLOT="0"
LICENSE="GPL-2"
KEYWORDS="~x86 ~amd64"

RDEPEND="virtual/x11
	virtual/opengl
	sys-apps/file
	media-tv/xawtv
    >=dev-libs/libxml2-2.4.23
	>=sys-devel/libtool-1.4.2
	>=media-libs/libpng-1.0.13
	ffmpeg? ( >=media-video/ffmpeg-0.4.9_pre1 )
	gd? ( >=media-libs/gd-2.0.1 )
	dvd? ( >=media-libs/libdvdnav-0.1.10 >=media-libs/libdvdread-0.9.4 >=media-libs/libdvdcss-1.2.0 )
	a52? ( >=media-libs/a52dec-0.7.4 )
	cdparanoia? ( >=media-sound/cdparanoia-3.9.8 )
	jpeg? ( >=media-libs/jpeg-6b )
	encode? ( >=media-sound/lame-3.92 )
	imagemagick? ( >=media-gfx/imagemagick-6.2.2 )
	lirc? ( >app-misc/lirc-0.6.3 )
	live? ( >=media-plugins/live-2004.11.09 )
	mad? ( >=media-libs/libmad-0.14.2b )
	mpeg2dec? ( >=media-libs/libmpeg2-0.4.0b )
	ncurses? ( >=sys-libs/ncurses-5.2 )
	dvb? ( >=media-libs/libdvb-0.4.1 )
	qt? ( >=x11-libs/qt-3.3.2 )
	shout? ( >=media-libs/libshout-2.0 )
	vorbis? ( >=media-libs/libvorbis-1.0.1 )
	fftw? ( =sci-libs/fftw-2* )
	xercesc? ( >=dev-libs/xerces-c-2.6.0 )
	l1394? ( >=media-libs/l1394-0.2.4 )
	alsa? ( virtual/alsa media-sound/aumix )"

DEPEND="${RDEPEND}"

pkg_setup() {
	if is_2_4_kernel; then
		if ! has_version media-tv/linuxtv-dvb; then
			eerror "For 2.4 kernels, media-tv/linuxtv-dvb is required"
		fi
	fi
}

src_unpack() {
	unpack ${A}
	cd ${S}
	PNEW="nmm-0.9.0"
	
	echo ""
	einfo "Patching nmm"
	# patch to have --enable-mpeg2 et --disable-mpeg2
	epatch ${FILESDIR}/${PNEW}-disable-mpeg2dec-nmm-configure.in.patch07

	# patch to locate dvb headers
	epatch ${FILESDIR}/${PNEW}-dvb-header-patch

	# patch to correct dvb-node includes
	epatch ${FILESDIR}/${PNEW}-dvb-api-patch

	# patch for live detection and size_t bug on x86_64
	epatch ${FILESDIR}/${PNEW}-live-patch-17

	# patch bug ffmpeg bad prototypage
	epatch ${FILESDIR}/${PNEW}-ffmpeg-bug.patch

	#patch for new api chanfre for ffmpeg cvs 
	epatch ${FILESDIR}/${PNEW}-FFAVFormat.patch
	epatch ${FILESDIR}/${P}-FFMpegDecodeNode-2.patch
	epatch ${FILESDIR}/${PNEW}-FFMpegEncodeNode.patch

	#debug patch
	epatch ${FILESDIR}/${P}-MP3ReadNode-3.patch

	#Configure.in patch for shout
	epatch ${FILESDIR}/${P}-configure.in-shout.patch
	
	# support for x86_64
	if [ 64-bit ]; then
		echo ""
		einfo "64-bit detected"

		# patch Event files
		#epatch ${FILESDIR}/${PNEW}-x86-64-Plugin.cpp.patch
		epatch ${FILESDIR}/${PNEW}-x86-64-support-event.patch

		# patch bufferstream
		epatch ${FILESDIR}/x86_64-long-BufferStream.patch

		# patch magicnumbervaluestream
		epatch ${FILESDIR}/x86_64-long-MagicNumberValueStream.patch

		# patch stdovaluestream
		epatch ${FILESDIR}/x86_64-long-StdOValueStream.patch
	
		# patch stdstream
		epatch ${FILESDIR}/x86_64-long-StdStream.patch
	
		# patch valuestream
		epatch ${FILESDIR}/x86_64-long-ValueStream.patch

		# patch xmlvaluestream
		epatch ${FILESDIR}/x86_64-long-XMLValueStream.patch

		# patch netstream
		epatch ${FILESDIR}/x86_64-long-NetStream.patch

		# patch iclicvaluestream
		epatch ${FILESDIR}/x86_64-long-IClicValueStream.patch

		# patch MAKEFILE for tvplugins and camera for x86_64 arch
		epatch ${FILESDIR}/${PNEW}-x86-64-tvcard.patch
		epatch ${FILESDIR}/${PNEW}-x86-64-itv.patch
		epatch ${FILESDIR}/${PNEW}-x86-64-contrib_tvcard.patch
		epatch ${FILESDIR}/${PNEW}-x86-64-camera.patch
		
		epatch ${FILESDIR}/x86-64-long-MagicNumberValueStream-5.patch
	    epatch ${FILESDIR}/x86-64-long-ComplexValue.patch

		echo ""
		einfo "patch for networking"
		epatch ${FILESDIR}/${P}-TCPStrategy.cpp.patch
		epatch ${FILESDIR}/${P}-UDPStrategy.cpp.patch
	fi

	
	echo ""
	einfo "patch for compilation warning"

	#patch for warning compilation 
	epatch ${FILESDIR}/${PNEW}-config-comparison.patch
	epatch ${FILESDIR}/${PNEW}-DirReader-comparison.patch
	epatch ${FILESDIR}/${PNEW}-eit-comparison.patch
	epatch ${FILESDIR}/${PNEW}-GraphURL-comparison.patch
	epatch ${FILESDIR}/${PNEW}-HTTPProtocolReader-comparison.patch
	epatch ${FILESDIR}/${PNEW}-inet_ntop.c-null.patch
	epatch ${FILESDIR}/${PNEW}-JPEGEncodeNode-comparison.patch
	epatch ${FILESDIR}/${PNEW}-LADSPANode-comparison.patch
	epatch ${FILESDIR}/${PNEW}-PNGReadNode-comparison.patch
	epatch ${FILESDIR}/${PNEW}-recording-redifined.patch
	epatch ${FILESDIR}/${PNEW}-tools-redifined.patch
	epatch ${FILESDIR}/${PNEW}-TVCardReadNode-comparison.patch
	epatch ${FILESDIR}/${PNEW}-TVGXercescDatabase-comparison.patch
	epatch ${FILESDIR}/${PNEW}-WhiteNoiseNode-comparison.patch
	epatch ${FILESDIR}/${PNEW}-XMLParser-comparison.patch
	epatch ${FILESDIR}/${PNEW}-xmltvhelper-comparison.patch
	epatch ${FILESDIR}/${PNEW}-LCDWriter-comparison.patch
	epatch ${FILESDIR}/${PNEW}-NMMConfig-comparison.patch
	epatch ${FILESDIR}/${PNEW}-ProgramDescription-comparison.patch
	epatch ${FILESDIR}/${PNEW}-ProgramFilter-comparison.patch
	epatch ${FILESDIR}/${PNEW}-SPUClass-pointercast.patch
	epatch ${FILESDIR}/${PNEW}-WindowSelectorPlaylist-comparison.patch
	epatch ${FILESDIR}/${PNEW}-epg-comparison.patch
	epatch ${FILESDIR}/${PNEW}-font-comparison.patch
	#epatch ${FILESDIR}/${PNEW}-grabdvd-comparison.patch
	epatch ${FILESDIR}/${PNEW}-playlist-comparison.patch
	epatch ${FILESDIR}/${PNEW}-transcode-comparison.patch
	epatch ${FILESDIR}/${PNEW}-tvtimer-comparison.patch
	
	#epatch ${FILESDIR}/${PNEW}-TCPStrategy-3.patch

	# replacement unsigned by size_t
	#epatch ${FILESDIR}/${P}-BufferStream.patch
	#epatch ${FILESDIR}/${P}-ChannelDescription.hpp.patch
	#epatch ${FILESDIR}/${P}-Event.patch
	#epatch ${FILESDIR}/${P}-IClicValueStream.patch
	#epatch ${FILESDIR}/${P}-MPEGVideoDecodeNode.hpp.patch
	#epatch ${FILESDIR}/${P}-MagicNumberValueStream.patch
	#epatch ${FILESDIR}/${P}-NetStream.patch
	#epatch ${FILESDIR}/${P}-ProgramDescription.hpp.patch
	#epatch ${FILESDIR}/${P}-QueuedInputJack.patch
	#epatch ${FILESDIR}/${P}-QueuedOutputJack.patch
	#epatch ${FILESDIR}/${P}-SPUClass.patch
	#epatch ${FILESDIR}/${P}-StdOValueStream.patch
	#epatch ${FILESDIR}/${P}-StdStream.patch
	#epatch ${FILESDIR}/${P}-StreamQueue.patch
	#epatch ${FILESDIR}/${P}-TableOfContents.patch
	#epatch ${FILESDIR}/${P}-XMLValueStream.patch
	#epatch ${FILESDIR}/${P}-TCPGeneric.cpp.patch
	#epatch ${FILESDIR}/${P}-Value.hpp.patch
	#epatch ${FILESDIR}/${P}-ValueStream.hpp.patch
	#epatch ${FILESDIR}/${P}-benchmarks.cpp.patch
	#epatch ${FILESDIR}/${P}-config.cpp.patch
	#epatch ${FILESDIR}/${P}-grabdvd.cpp.patch
	
	# generation of the configure
	export WANT_AUTOCONF=2.5
	export WANT_AUTOMAKE=1.4

	#elibtoolize
	
	# generation of the configure
	aclocal || die "aclocal failed"
	autoheader || die "autoheader failed"
	libtoolize --automake || die "libtoolize automake failed"
	automake -a || die "automake -a failed"
	autoconf || die "autoconf failed"

	libtoolize --copy --force || die

	# copy the good version of libtool 
	#elibtoolize 

}
	
src_compile() {

	local myconf="--disable-examples --disable-qregwizard
	--disable-qcamctrl --disable-qvcore --disable-ffmpeg-nmm --disable-lame-nmm
	--disable-magick-nmm --disable-mad-nmm --disable-xercesc-nmm
	--disable-mpeg2-nmm --disable-oggvorbis-nmm --disable-dvdread-nmm"

	use ffmpeg \
		&& myconf="${myconf} --enable-ffmpeg" \
		|| myconf="${myconf} --disable-ffmpeg"

	use gd \
		&& myconf="${myconf} --enable-gd" \
		|| myconf="${myconf} --disable-gd"

	use dvd \
		&& myconf="${myconf} --enable-dvdread --enable-dvdnav" \
		|| myconf="${myconf} --disable-dvdread --disable-dvdnav"

	use a52 \
		&&  myconf="${myconf} --enable-a52" \
		||  myconf="${myconf} --disable-a52"

	use cdparanoia \
		&&  myconf="${myconf} --enable-cdda" \
		||  myconf="${myconf} --disable-cdda"

	use jpeg \
		&&  myconf="${myconf} --enable-jpeg" \
		||  myconf="${myconf} --disable-jpeg"

	use encode \
		&&  myconf="${myconf} --enable-lame" \
		||  myconf="${myconf} --disable-lame"

	use imagemagick \
		&& myconf="${myconf} --enable-magick" \
		|| myconf="${myconf} --disable-magick"

	use lirc \
		&& myconf="${myconf} --enable-lirc" \
		|| myconf="${myconf} --disable-lirc"

	if use live; then
		myconf="${myconf} --enable-live --enable-streamingserver"
		local my_inc="/usr/lib/live"
		local my_lib="/usr/lib/live"

		LDFLAGS="-L${my_lib}/liveMedia -L${my_lib}/groupsock \
		-L${my_lib}/UsageEnvironment -L${my_lib}/BasicUsageEnvironment \
		${LDFLAGS}"

		CPPFLAGS="-I${my_inc}/liveMedia/include -I${my_inc}/groupsock/include \
		-I${my_inc}/UsageEnvironment/include \
		-I${my_inc}/BasicUsageEnvironment/include ${CPPFLAGS}"
	else
		myconf="${myconf} --disable-live --disable-streamingserver"
	fi

	use mad \
		&& myconf="${myconf} --enable-mad" \
		|| myconf="${myconf} --disable-mad"

	use mpeg2dec \
		&& myconf="${myconf} --enable-mpeg2" \
		|| myconf="${myconf} --disable-mpeg2"

	use ncurses \
		&& myconf="${myconf} --enable-ncurses" \
		|| myconf="${myconf} --disable-ncurses"

	use dvb \
		&& myconf="${myconf} --enable-dvb" \
		|| myconf="${myconf} --disable-dvb"

	use alsa \
		&& myconf="${myconf} --enable-alsa" \
		|| myconf="${myconf} --disable-alsa"

	use qt \
		&& myconf="${myconf} --enable-qt3" \
		|| myconf="${myconf} --disable-qt3"
		
	use shout \
		&&  myconf="${myconf} --enable-shout" \
		||  myconf="${myconf} --disable-shout"

	use vorbis \
		&&  myconf="${myconf} --enable-vorbis" \
		||  myconf="${myconf} --disable-vorbis"

	use fftw \
		&& myconf="${myconf} --enable-librfftw" \
		|| myconf="${myconf} --disable-librfftw"

	use xercesc \
		&& myconf="${myconf} --enable-xercesc" \
		|| myconf="${myconf} --disable-xercesc"

	use l1394 \
		&& myconf="${myconf} --enable-l1394 --enable-raw1394" \
		|| myconf="${myconf} --disable-l1394 --disable-raw1394"

	use mmbox \
		&& myconf="${myconf} --enable-mmbox" \
		|| myconf="${myconf} --disable-mmbox"

	CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}" econf ${myconf} || die "Configuration failed."
	emake CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" CPPFLAGS="${CPPFLAGS}" || die "emake failed."
	
}

src_install() {

	einstall || die "Installation failed."
}
